package fe.pong.tennis.screens;

import fe.pong.tennis.fisica.ManagerCollision;
import fe.pong.tennis.Score;
import fe.pong.tennis.sound.SoundManager;
import flash.display.Sprite;
import fe.pong.tennis.gameElements.GameElement;
import fe.pong.tennis.gameElements.Image;
import fe.pong.tennis.gameElements.Pelota;
import fe.pong.tennis.gameElements.Player;
import fe.pong.tennis.gameElements.Personaje;
import fe.pong.tennis.input.InputConfiguration;
import fe.pong.tennis.gameElements.Image;
import flash.text.TextField;
import flash.text.TextFormat;

class GamePlayScreen extends Screen {

//después tenemos que revisar si el arreglo de no activos sirve o esta al pedo, porque para mi que esta al pedo. Seba
	//private var _noActivos : Array<GameElement>;
	private var _activos : Array<GameElement>;
	private var _player1:Player;
	private var _player2:Player;
	private var pelota: Pelota;
	private var curt: Image;
	private var _managerDeColisiones:ManagerCollision;
	private var sm : ScreenManager;
	private var result : Score;
	private var homeResult: TextField;
	private var guestResult: TextField;
	private var winner : TextField;
	private var soundManager : SoundManager;

	public function new(sm:ScreenManager, inputConfig1: InputConfiguration, inputConfig2: InputConfiguration) {
		super(sm);
		_activos = new Array<GameElement>();
		//_noActivos = new Array<GameElement>();
		this.sm = sm;

		var background: GameElement = new Image("images/curt.png", sm.getHeigth());
		background.width = sm.getWidth();
		addChild(background);

		_player1 = new Player(inputConfig1, sm.getWidth() / 2, 15, sm.getWidth(), sm.getHeigth(), 20, 0);
		_player2 = new Player(inputConfig2, sm.getWidth() / 2, sm.getHeigth() - 15 , sm.getWidth(), sm.getHeigth(), 20, 0);
		_player1.y += _player1.height;
		_player2.y -= _player2.height;

		_managerDeColisiones = new ManagerCollision();
		soundManager = new SoundManager();

		pelota = new Pelota(
			sm.getWidth(),
			sm.getHeigth(),
			sm.getWidth() / 2,
			sm.getHeigth() / 2,
			Math.PI/2,
			30,
			0);

		_managerDeColisiones.addElement(_player1);
		_managerDeColisiones.addElement(_player2);

		addChild(_player1);
		addChild(_player2);		
		addChild(pelota);

		_activos.push(_player1);
		_activos.push(_player2);
		_activos.push(pelota);
		
		result = new Score();
		this.homeResult = new TextField();		
		this.guestResult = new TextField();
		this.winner = new TextField();
		
		this.addChild(homeResult);
		this.addChild(guestResult);
		this.addChild(winner);
	}

	public override function updateLogic(time:Float){
		for(activo in _activos){
			activo.updateLogic(time);
		}
		if(!result.getWinner(1)){
			var player: Player = null;
			if (pelota.visible) {
				player = _managerDeColisiones.getCollision(pelota);
				if (player != null) {
					pelota.shoot(player);
					soundManager.hitBall();
				}
			}
			
			if (pelota.getHighBounce()) 			
				this.result.addHomePoint();
			else
				if (pelota.getLowBounce()) 
					this.result.addGuestPoint();			
			var auxhome : String = result.getHomeResult();
			writeResult(sm.width/2-200, sm.height/2-50, 200, 50, 0x00FF00, auxhome , homeResult);
			var auxguest : String = result.getGuestResult();
			writeResult(sm.width / 2, sm.height / 2 , 200, 50, 0x00FF00, auxguest , guestResult);
		}
		else {					
			pelota.visible = false;
			var aux : String;
			aux = result.getState() + "";			
			writeResult(sm.width/2-400, sm.height/2-100, 200, 50, 0x00FF00, aux , winner);
			writeResult(0, 0, 200, 50, 0x00FF00, aux , winner);
			var auxlocal : String = result.getHomeResult();
			writeResult(sm.width/2-200, sm.height/2-50, 200, 50, 0x00FF00, auxlocal , homeResult);
			var auxguest : String = result.getGuestResult();
			writeResult(sm.width / 2, sm.height / 2 , 200, 50, 0x00FF00, auxguest , guestResult);
		}

	}

	override public function init():Void {
		_activos = new Array<GameElement>();
		//_noActivos = new Array<GameElement>();

		_activos.push(_player1);
		_activos.push(_player2);
		_activos.push(pelota);
	}

	public function writeResult(posX: Float, posY: Float, w:Int, h:Int, color:Int, labelText:String, _result: TextField) {
		var tf = new TextFormat(openfl.Assets.getFont('fonts/megadeth-cryptic.ttf').fontName);
		tf.size = h;
		tf.color = 0xFFFFFF;
		tf.bold = true;
		tf.align = flash.text.TextFormatAlign.CENTER;

		//_result = new TextField();		
		_result.x = posX;
		_result.y = posY;
		_result.width = w;
		_result.height = h;
		_result.text = labelText;
		_result.setTextFormat(tf);
		//_result.y = (h - _result.textHeight);
		//this.addChild(_texto);
		}

}